package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsInspectCollectCfgMapper;
import cc.rengu.igas.bsps.common.dao.BspsMchntBaseInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsInspectCollectCfgMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBaseInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.InspectCollectCfg;
import cc.rengu.igas.bsps.common.entity.MchntBaseInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.InspectCollectCfgInfoBean;
import cc.rengu.igas.bsps.facade.request.InspectCollectConfigQueryRequest;
import cc.rengu.igas.bsps.facade.response.InspectCollectConfigQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxuran on 2020/04/08
 */
public class InspectCollectConfigQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        InspectCollectConfigQueryRequest inspectCollectConfigQueryRequest = new InspectCollectConfigQueryRequest();
        ConvertUtil.convertOutput(inspectCollectConfigQueryRequest);
        return inspectCollectConfigQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        InspectCollectConfigQueryRequest inspectCollectConfigQueryRequest = (InspectCollectConfigQueryRequest) request;
        InspectCollectConfigQueryResponse inspectCollectConfigQueryResponse = new InspectCollectConfigQueryResponse();
        /*获取请求参数信息法人行号，用户标识，巡检方式，商户属性，商户号信息*/
        String instId = inspectCollectConfigQueryRequest.getHeader().getInstId();
        String userId = inspectCollectConfigQueryRequest.getUserId();
        String inspectType = inspectCollectConfigQueryRequest.getInspectType();
        String mchntAttr = inspectCollectConfigQueryRequest.getMchntAttr();
        String mchntNo = inspectCollectConfigQueryRequest.getMchntNo();
        /*判断参数必填性*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(inspectType) || StringUtil.isEmptyOrNull(inspectType) || (StringUtil.isEmptyOrNull(mchntNo) && StringUtil.isEmptyOrNull(mchntAttr))) {
            rglog.error("巡检任务配置查询请求参数必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        if (StringUtil.isEmptyOrNull(mchntAttr)) {
            BspsMchntBaseInfoMapper bspsMchntBaseInfoMapper = new BspsMchntBaseInfoMapperImpl();
            MchntBaseInfo mchntBaseInfo = bspsMchntBaseInfoMapper.selectBspsMchntBaseInfoByPrimaryKey(instId, mchntNo);
            if (null == mchntBaseInfo) {
                rglog.error("商户基本信息查询失败！");
                throw new BizException(RespCodeEnum.QUERY_MCHNT_BASE_INFO_ERROR.getRespCode(), RespCodeEnum.QUERY_MCHNT_BASE_INFO_ERROR.getRespDesc());
            }
            mchntAttr = mchntBaseInfo.getMchntAttr();
        }
        /*查询巡检任务配置采集信息*/
        List<InspectCollectCfgInfoBean> inspectCollectCfgInfoList = new ArrayList<>();
        BspsInspectCollectCfgMapper bspsInspectCollectCfgMapper = new BspsInspectCollectCfgMapperImpl();
        List<InspectCollectCfg> inspectCollectCfgList = bspsInspectCollectCfgMapper.selectInspectCollectCfgByInspectTypeAndMchntAttr(instId, inspectType, mchntAttr);
        if (null != inspectCollectCfgList && !inspectCollectCfgList.isEmpty()) {
            inspectCollectCfgList.forEach(item -> convInspectColletCfgInfo(item, inspectCollectCfgInfoList));
        }
        inspectCollectConfigQueryResponse.setInspectCollectCfgInfoList(inspectCollectCfgInfoList);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<InspectCollectConfigQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(inspectCollectConfigQueryResponse);
        return bizResponse;
    }

    /*
     * 巡检任务配置采集信息转换
     * inspectCollectCfg 巡检任务配置采集信息
     * */
    private void convInspectColletCfgInfo(InspectCollectCfg inspectCollectCfg, List<InspectCollectCfgInfoBean> inspectCollectCfgInfoList) {
        InspectCollectCfgInfoBean inspectCollectCfgInfo = new InspectCollectCfgInfoBean();
        /*采集信息ID赋值*/
        inspectCollectCfgInfo.setCollectId(inspectCollectCfg.getCollectId());
        /*采集信息类型赋值*/
        inspectCollectCfgInfo.setCollectType(inspectCollectCfg.getCollectType());
        /*采集信息描述赋值*/
        inspectCollectCfgInfo.setCollectDesc(inspectCollectCfg.getCollectDesc());
        /*列表添加对象*/
        inspectCollectCfgInfoList.add(inspectCollectCfgInfo);
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InspectCollectConfigQueryResponse inspectCollectConfigQueryResponse = (InspectCollectConfigQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(inspectCollectConfigQueryResponse);
    }
}
